package com.itfenghuang.service.imp;

import com.itfenghuang.bean.User;
import com.itfenghuang.mapper.UserMapper;
import com.itfenghuang.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserServiceImp implements UserService {
    @Override
    public List<User> selectUser() {
        InputStream is=null;
        SqlSession sqlSession=null;
        List<User> users=null;
        try {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            //2.获取sqlsession工厂对象
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            //3.获取sqlsession对象
            sqlSession = build.openSession();
            //4.通过SqlSession对象获取StudentMapper接口的实现类对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //5.通过对象调用对应的增删改查方法
            users = mapper.selectUser();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        System.out.println(users+"8888888");
        return users;
    }
}
